package net.csdn.business.common.config;

import net.csdn.business.common.constant.RetryConstants;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

/**
 * 重试机制配置
 *
 * @author wanghang
 * @version 1.0
 * @date 2022/7/7 20:50
 */
public class RetryConfig {

    public static RetryTemplate configRetryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        // 设置重试策略
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(RetryConstants.RETRY_MAX_ATTEMPTS);
        retryTemplate.setRetryPolicy(retryPolicy);

        // 设置退避策略
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(RetryConstants.RETRY_DELAY_MS);
        backOffPolicy.setMultiplier(RetryConstants.RETRY_MULTIPLIER);
        backOffPolicy.setMaxInterval(RetryConstants.MAX_RETRY_DELAY_MS);
        retryTemplate.setBackOffPolicy(backOffPolicy);

        return retryTemplate;
    }

}